package com.zuipin.quartz;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.zuipin.entity.SysUser;
import com.zuipin.framework.result.Result;
import com.zuipin.service.IOrderService;
import com.zuipin.util.DateUtils;

@Configuration
public class CancelOrderJob extends QuartzJobBean {
	@Resource
	private IOrderService	orderService;
	private Logger			log			= Logger.getLogger(CancelOrderJob.class);
	public static final int	CANCEL_TIME	= 48;
	
	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
		log.info("取消订单--定时器---start");
		List<Map<String, Object>> list = orderService.canCancelOrder();
		for (Map<String, Object> map : list) {
			Result res = new Result();
			try {
				Long diffHour = DateUtils.getDateDiffHour(
						DateUtils.parseFullTime(DateUtils.getCurrentDateTime()),
						DateUtils.parseFullTime(String.valueOf(map.get("createTime"))));
				if (diffHour.intValue() >= CANCEL_TIME) {
					orderService.cancelOrder(Long.valueOf(String.valueOf(map.get("payId"))),
							SysUser.QUARTZ, null, null, res, "超过48小时未支付，系统自动取消。");
					log.info("取消订单--定时器---payId:" + map.get("payId") + "--success");
				}
				
			} catch (Exception e) {
				e.fillInStackTrace();
				log.error("定时器  ==取消订单失败付款单号为：：" + map.get("payId") + "..异常信息为" + e.getMessage());
			}
		}
		log.info("取消订单--定时器---end");
	}
	
	public IOrderService getOrderService() {
		return orderService;
	}
	
	public void setOrderService(IOrderService orderService) {
		this.orderService = orderService;
	}
	
}